package ar.com.ironsoft.javaopenauth.oauth;

import java.io.IOException;
import java.util.Properties;
import java.util.Set;

import ar.com.ironsoft.javaopenauth.dto.OAuthToken;
import ar.com.ironsoft.javaopenauth.exceptions.JavaOpenAuthException;
import ar.com.ironsoft.javaopenauth.utils.ServletActionType;

public abstract class OAuthService implements IOAuthService{
	protected String controllerUrlMapping;
	private Set<ServletActionType> registeredActions;
	private Properties oauthProperties;
	private Properties oauthClientProperties;
	protected OAuthToken oauthToken = new OAuthToken();
	
	public OAuthService(String controllerUrlMapping,
			Set<ServletActionType> registeredActions) {
		super();
		this.controllerUrlMapping = controllerUrlMapping;
		this.registeredActions = registeredActions;
		loadPropertiesFiles();
	}
	
	public OAuthService(String controllerUrlMapping,
			Set<ServletActionType> registeredActions,String clientKeysFilename) {
		super();
		this.controllerUrlMapping = controllerUrlMapping;
		this.registeredActions = registeredActions;
		loadPropertiesFiles();
		loadClientProperties(clientKeysFilename);
	}

	private void loadPropertiesFiles() {
		try {
			oauthProperties = new Properties();
			oauthProperties.load(getClass().getClassLoader().getResourceAsStream("oauth_config.properties"));
			
		} catch (IOException e) {
			throw new JavaOpenAuthException(
					"The oauth config cannot be loaded.", e);
		}
	}
	
	public void loadClientProperties(String propertiesFileName) {
		try {
			oauthClientProperties = new Properties();
			oauthClientProperties.load(getClass().getClassLoader().getResourceAsStream(propertiesFileName));
			
		} catch (IOException e) {
			throw new JavaOpenAuthException(
					"Your oauth keys cannot be loaded.", e);
		}
	}

	
	public String getControllerUrlMapping() {
		return controllerUrlMapping;
	}

	public void setControllerUrlMapping(String controllerUrlMapping) {
		this.controllerUrlMapping = controllerUrlMapping;
	}

	public Set<ServletActionType> getRegisteredActions() {
		return registeredActions;
	}

	public void setRegisteredActions(Set<ServletActionType> registeredActions) {
		this.registeredActions = registeredActions;
	}

	public Properties getOauthProperties() {
		return oauthProperties;
	}

	public void setOauthProperties(Properties oauthProperties) {
		this.oauthProperties = oauthProperties;
	}
	
	public String getApiKey(){
		return oauthClientProperties.getProperty(controllerUrlMapping + ".api_key");
	}
	
	public String getConsumerKey(){
		return oauthClientProperties.getProperty(controllerUrlMapping + ".consumer_key");
	}
	
	public String getConsumerSecret(){
		return oauthClientProperties.getProperty(controllerUrlMapping + ".consumer_secret");
	}
	
	public String getCallbackURL(){
		return oauthClientProperties.getProperty(controllerUrlMapping + ".callback_url");
	}
	
	public String getCallbackAppURL(){
		return oauthClientProperties.getProperty(controllerUrlMapping + ".callback_app");
	}

	public String getAccessTokenURL(){
		return oauthProperties.getProperty(controllerUrlMapping + ".url.access_token");
	}
	
	public String getRefreshTokenURL(){
		return oauthProperties.getProperty(controllerUrlMapping + ".url.refresh_token");
	}
	
	public OAuthToken getToken() {
		return oauthToken;
	}
}
